package com.maridress.maridress.dependencies.network.interceptor;

import com.maridress.maridress.common.util.SignUtils;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by tenmei on 16/9/10.
 */
public class ApiInterceptor implements Interceptor {
    private final String mApiKey;
    private final String mApiSecret;

    public ApiInterceptor(String apiKey, String apiSecret) {
        mApiKey = apiKey;
        mApiSecret = apiSecret;
    }

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        String marvelHash = SignUtils.generateMarvelHash(mApiKey, mApiSecret);
        Request oldRequest = chain.request();
        // 添加新的参数
        HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                .newBuilder()
                .scheme(oldRequest.url().scheme())
                .host(oldRequest.url().host())
                .addQueryParameter("token", marvelHash);
//                .addQueryParameter(MarvelService.PARAM_API_KEY, mApiKey)
//                .addQueryParameter(MarvelService.PARAM_TIMESTAMP, MarvelApiUtils.getUnixTimeStamp())
//                .addQueryParameter(MarvelService.PARAM_HASH, marvelHash);
//        .add


        // 新的请求
        Request newRequest = oldRequest.newBuilder()
                .method(oldRequest.method(), oldRequest.body())
                .url(authorizedUrlBuilder.build())
                .build();

        return chain.proceed(newRequest);
    }

}
